package com.qianfeng.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.bean.AjaxResponse;
import com.qianfeng.openapi.web.master.bean.TableData;
import com.qianfeng.openapi.web.master.pojo.AdminUser;
import com.qianfeng.openapi.web.master.service.AdminUserService;
import com.qianfeng.openapi.web.master.utils.Constacts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/1 11:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/admin/")
@CrossOrigin(allowCredentials = "true") //加此注解允许跨域访问
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;


    @RequestMapping("/login")
    public AjaxResponse login(HttpSession session, AdminUser adminUser){
        AdminUser user = adminUserService.findUser(adminUser);
        if(user == null){
            return new AjaxResponse(false,"登录失败，不存在用户");
        }
        session.setAttribute(Constacts.LOGIN_ADMIN_USER,user);
        return new AjaxResponse(true,"登录成功");
    }



    @RequestMapping("/table")
    public TableData<AdminUser> table(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        PageInfo<AdminUser> pageInfo = adminUserService.getAllAdminUsers(page, limit);
        TableData<AdminUser> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;
    }

    @RequestMapping("/add")
    public AjaxResponse add(AdminUser adminUser){
        try {
            adminUserService.insertAdminUser(adminUser);
            return new AjaxResponse(true,"添加成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return new AjaxResponse(false,"添加失败");
        }
    }


    @RequestMapping("/update")
    public AjaxResponse update(AdminUser adminUser){
        try {
            adminUserService.updateAdminUser(adminUser);
            return new AjaxResponse(true,"更新成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return new AjaxResponse(false,"更新失败");
        }
    }

    @RequestMapping("/delete")
    public AjaxResponse delete(Integer[] ids){
        try {
            adminUserService.deleteAdminUsers(ids);
            return new AjaxResponse(true,"删除成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return new AjaxResponse(false,"删除失败");
        }
    }

    @RequestMapping("/search")
    public TableData<AdminUser> search(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5") Integer limit,
                                        String email,String realName,Integer status){
        PageInfo<AdminUser> pageInfo = adminUserService.selectAdminUserByCondition(page, limit,email, realName, status);
        TableData<AdminUser> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;

    }

    @RequestMapping("/tree")
    public List<AdminUser> tree(){
        return adminUserService.getAdminUsers();
    }
}
